package com.joinway.framework.support.logging;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.joinway.framework.support.audit.constant.AuditConstants;

public class FlowInterceptor extends HandlerInterceptorAdapter {

	private final static Logger log = LoggerFactory.getLogger(FlowInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// add audit key to logger
    	String flow = StringUtils.split(UUID.randomUUID().toString(), "-")[0];
    	
    	request.getSession().setAttribute(AuditConstants.FLOW_KEY, request.getSession().getId() + "." + flow);
    	MDC.put(AuditConstants.FLOW_KEY, flow);
    	
		return super.preHandle(request, response, handler);
	}
}
